(function($){
    $.fn.extend({
	preloader: function(options) {

	    var defaults = {

		img:	'templates/loading.gif',
		alt:	'wczytuję...',
		w:	220,
		h:	19,
		bgcolor: '#000',
		akcja: 'start'

	    };

	    var options = $.extend(defaults, options);

	    return this.each(function() {
		var o = options;
		var obj = $(this);

		if (o.akcja == 'stop') {
		$('#loader-img').fadeOut("normal", function(){
		    $('#loader-img').remove();
		});
		$('#loader-bg').fadeOut("normal", function(){
		    $('#loader-bg').remove();
		});

		} else {

		if (self.pageYOffset)
		{
		    yScroll = self.pageYOffset;
		    xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop)
		{
		    yScroll = document.documentElement.scrollTop;
		    xScroll = document.documentElement.scrollLeft;
		} else if (document.body)
		{
		    yScroll = document.body.scrollTop;
		    xScroll = document.body.scrollLeft;
		}
		var $body = $(this.ie6 ? document.body : document);
		$("body").append('<div id="loader-bg"></div>');
		$('#loader-bg').css(
		{
		    display: 'none',
		    position: 'absolute',
		    top: '0%',
		    left: '0%',
		    width:$body.width(),
		    height:$body.height(),
		    backgroundColor: o.bgcolor,
		    zIndex: 1001,
		    opacity:0

		});

		var img = $('<img src="'+o.img+'" alt="'+o.alt+'" id="loader-imgages" />');
		var wh = $(window).height();
		var ww = $(window).width();
		var ch = o.h;
		var cw = o.w;
		var nw = xScroll+(ww-cw)/2;
		var nh = yScroll+(wh-ch)/2.5;

		$("body").append('<div id="loader-img"></div>');
		$('#loader-img').css({
		    top:nh,
		    display: 'none',
		    position: 'absolute',
		    left: nw,
		    width: cw,
		    height: ch,
		    border: 'none',
		    zIndex:1002,
		    opacity:0
		});



		$("#loader-img").append(img);

		    $('#loader-bg').css({"display":"block"});
		    $('#loader-img').css({"display":"block"});
		    $('#loader-bg').animate({"opacity":"0.8"},"normal");
		    $('#loader-img').animate({"opacity":"1"},"normal");

		}

	    });
	}
    });
})(jQuery);